<?php

if($_GET['ajaxLoadSlider'] == 1){
		$page = (int) $_POST['page'];
		
		if(!$page || $page == 0){
			echo json_encode(array('error'=>'Có lỗi xảy ra, vui lòng thử lại.'));
			exit();
		}
		
		$alias = $_POST['alias'];

		
		switch ($alias){
			case 'decu':
				$cache=new cache;
				$_GET['page'] = (int) $_POST['page'];
				$sql ="select c.id as chap_id,c.ten,c.seourl,c.index_show,m.name,m.id,m.alias,m.hinhanh from chapterk2 as c
		left join anime as m
		on c.manga=m.id 
		where c.home=1 order by ngaythem desc,c.id desc";
		$sql2="select id from chapterk2 where home=1";
			
				$pager = new Pagination($sql,5, 4, '', 300);
				$pager->setDebug(false);
				$data['decu2'] = $pager->paginate($cache,$sql2);
				foreach($data['decu2'] as $row){
					$row['ep_moinhat']=$row['ten'];
					$row['ep_moinhaturl']=url_anime_episode($row['chap_id'],$row['seourl']);

					$data['decu'][]=$row;
				}
			
				if(!$data['decu']){
					echo json_encode(array('error'=>'Hết dữ liệu!'));
					exit();
				}

				echo json_encode(array('content'=>manga_render_desktop_items($data['decu'])));
				break;
				case 'moi':
				$cache=new cache;
				$_GET['page'] = (int) $_POST['page'];
				
				$sql ="select id  from anime where  duyet=1 order by id desc";
				$sql2="select id from anime  where  duyet=1";
			
				$pager = new Pagination($sql,10, 4, '', 300, "anime_1");
				$pager->setDebug(false);
				$data['decu'] = $pager->paginate($cache,$sql2);
				
			
				if(!$data['decu']){
					echo json_encode(array('error'=>'Hết dữ liệu!'));
					exit();
				}

				echo json_encode(array('content'=>manga_render_desktop_items($data['decu'])));
				break;
		
			
				
		}
		exit();
		
	}
	
	
	if(isset($_GET['link_check'])){
		//Check link
		$link=$_GET['link_check'];
		$link=str_replace(" ","%20",$link);
		
		if(strpos($link,"blogspot.com")!==false){
			$pos=substr(strrchr($link,'/'),1);
		
			$link=str_replace($pos,"1.jpg",$link);
			
		}
		
		$surl="url=".urlencode($link);
		$url = 'http://anime24h.vn/picasa/index4.php?c='.date('U');
		$resultContent = mycurl($surl,$url);
		$r = urldecode($resultContent);
		
		
		
		
		//$b=@getimagesize($link);
		if($r=='fail'){
			//File fail
			echo "file fail ".$link;
			$db=new db;
			$db->connect();
			$sql="update chapterk2 set check_error=1,check_link=1 where id=".intval($_GET['cid']);
			$sql2="update chapterk_content set check_error=1,check_link=1 where chap_id=".intval($_GET['cid']);
			//echo $sql;
			$db->nonquery($sql);
			echo mysql_error();
			$db->nonquery($sql2);
			echo mysql_error();
			$db->close();
		}else{
				echo "file oke ".$link;
			$db=new db;
			$db->connect();
			$sql="update chapterk2 set check_link=1,check_error=0 where id=".intval($_GET['cid']);
			$sql2="update chapterk_content set check_link=1, check_error=0 where chap_id=".intval($_GET['cid']);
			$db->nonquery($sql);
			$db->nonquery($sql2);
			echo mysql_error();
			//echo $sql;
			$db->close();
		}
		$id=$_GET['cid'];
		$sub=$id%100;
		$cache=new cache;
		$cache->flush('manga_get_chapter_detail'.$id,'chapter/'.$sub);
		echo $sql;
		
	}
	
	if(isset($_GET['resource'])){
			$id=$_GET['id'];
			if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest') {
				echo "not ajax";
       			die(); // this function is to send any response header , you can even use it for any other purpose as well
   			 }
			 
		
	//Fetch resource ------------
			
			$db=new Model;
			//---------- GET MANGA INFORMATION
		
			$db->query("select op,source,manga,check_link,check_error from chapterk2 where id=".$id);
			$row=$db->fetchOne();	
		
	
						if($row['op']==1){
						
							$db->query("select * from chapterk_content where chap_id=".$id);
							//echo mysql_error();
							if($db->num_rows()>0){
								$r2=$db->fetchOne();
								$db->query("select * from chapterk3_content where chap_id=".$id);
								$num=$db->num_rows();
								if($r2['server']=='manga24h.com'&&$num>0&$r2['check_link']==1&&$r2['check_error']==0){
								
									//GET TU TABLE CHAPTERK3_CONTENT
								
									$r2=$db->fetchOne();
									$row['chapter_link']=unserialize($r2['picasa']);
									$row['chapter_page']=$r2['chap_page'];
								}else{
								
											$row['chapter_link']=unserialize($r2['chap_content']);
											$link=$row['chapter_link'][0];
											
											if(strpos($link,'manga24h.com')!==false){
											$r5['server']='manga24h.com';
											$r5['error']=0;
											}else
											if(strpos($link,'upimgs.org')!==false){
											$r5['server']='upimgs';
											$r5['error']=1;
											}else
											if(strpos($link,'zdn.vn')!==false){
											$r5['server']='zing';
											$r5['error']=0;
											}
											else
											if(strpos($link,'upanh.com')!==false){
											$r5['server']='upanh';
											$r5['error']=1;
											}
											else
											if(strpos($link,'blogspot.com')!==false){
											$r5['server']='blogspot';
											$r5['error']=0;
											}else{
											$r5['server']='khac';
											$r5['error']=0;
											}
											
											
											
											$row['chapter_page']=$r2['chap_page'];
											
											
											$db->where("chap_id=".$id);
											$db->update("chapterk_content",$r5);
								}
								
							}
							
						}else{
							//Start get link page 
				
								echo "loi reousce ba no roi";
								//=========================Lay link image tu ngoai vao================
								/*
								$row['chapter_link']=array();
								$source=$row['source'];
								
									
									
								
								preg_match_all('/(img|src)\=(\"|\')[^\"\'\>]+/i', $source, $media);
								$datasource=preg_replace('/(img|src)(\"|\'|\=\"|\=\')(.*)/i',"$3",$media[0]);
								$images = array();
								$h=0;
								foreach($datasource as $url)
								{
											//preplace zing link......
														$new=preg_replace('/img.photo.zing.vn/',"d.f2.photo.zdn.vn",$url);
											  		   $bl=preg_replace('/lh3.ggpht.com/',"1.bp.blogspot.com",$new);
														$bl1=preg_replace('/lh4.ggpht.com/',"2.bp.blogspot.com",$bl);
														$bl2=preg_replace('/lh5.ggpht.com/',"3.bp.blogspot.com",$bl1);
														$bl3=preg_replace('/lh6.ggpht.com/',"4.bp.blogspot.com",$bl2);
														$bl4=preg_replace('/https:\/\/lh3.googleusercontent.com/',"http://3.bp.blogspot.com",$bl3);
														$bl5=preg_replace('/https:\/\/lh4.googleusercontent.com/',"http://2.bp.blogspot.com",$bl4);
														$bl6=preg_replace('/https:\/\/lh5.googleusercontent.com/',"http://1.bp.blogspot.com",$bl5);
														$bl7=preg_replace('/https:\/\/lh6.googleusercontent.com/',"http://4.bp.blogspot.com",$bl6);
													
														$bl8=preg_replace('/http:\/\/lh3.googleusercontent.com/',"http://3.bp.blogspot.com",$bl7);
														$bl9=preg_replace('/http:\/\/lh4.googleusercontent.com/',"http://2.bp.blogspot.com",$bl8);
														$bl10=preg_replace('/http:\/\/lh5.googleusercontent.com/',"http://1.bp.blogspot.com",$bl9);
														$bl11=preg_replace('/http:\/\/lh6.googleusercontent.com/',"http://4.bp.blogspot.com",$bl10);
														
														$new2=preg_replace('/data.manga24h.com\/manga/',"data2.manga24h.com/manga1",$bl11);
														$new3=preg_replace('/media3.manga24h.com/',"data2.manga24h.com",$new2);
														$new4=preg_replace('/data.manga24h.com\/data/',"data2.manga24h.com/data",$new3);
														$new5 = preg_replace('/\/s[0-9]*\//', '/s0/',$new4);
														
														
														$pos = strpos($new5, "blogspot.com");
														if ($pos === false) {
														   
														} else {
																	if(!preg_match("/\/s[0-9]*\//",$new5)&&!preg_match("/\/h[0-9]*\//",$new5)&&!preg_match("/\/w[0-9]*\//",$new5)){
																	$new5 = preg_replace('/\/[a-z0-9-_%\.]*\.(jpg|png|gif|jpeg)/i', '/s0/1.jpg',$new5);
																	}
																																		
														}
																												
																												
														
														
														array_push($row['chapter_link'],$new5);
														$h++;
	

											
		
									
	
								}
								$row['chapter_page']=$h;	
							
						
						
							
							
							
							// GET LINK OKE ROI, GIO INSERT VAO CHAPTERK_CONTENT
							$db->nonquery("delete from chapterk_content where chap_id=".$id);
							$datan=array("chap_content"=>serialize($row['chapter_link']),"chap_id"=>$id,"chap_page"=>$row['chapter_page']);
							$db->insert("chapterk_content",$datan);
							$db->nonquery("update chapterk2 set op=1 where id=".$id);
								*/
						
		}//End of else op
						
						//Check xem da check link chua ------------
	
						
						// GET STRING DE LOAD AJAX IMAGE
						$str='';
						$j=1;
						if($row['chapter_link'])
						foreach($row['chapter_link'] as $key => $value){
							$value=trim($value);
							if($j==$p){
							$str.=$value;
							}else{
							$str.=$value."|";
							}
							$j++;
									 
						}	
						
						$str = substr($str,0,-1);
						echo $str;	
	
	
	}
?>